const computedBehavior = require('miniprogram-computed').behavior
import myBehavior from './my-behavior'
const app = getApp()
Page({
  behaviors: [computedBehavior, myBehavior],
  computed: {
    tips(data) {
      return data.a + data.b
    }
  },
  data: {
    a: 'hello',
    b: 'world',
    students: [{
        id: 1,
        name: '小明',
        age: 18,
        gender: '男'
      },
      {
        id: 2,
        name: '小红',
        age: 17,
        gender: '女'
      },
      {
        id: 3,
        name: '小刚',
        age: 19,
        gender: '男'
      },
      {
        id: 4,
        name: '小丽',
        age: 18,
        gender: '女'
      },
      {
        id: 5,
        name: '小米',
        age: 19,
        gender: '女'
      },
    ],
    num: 5,
    msg: '小程序第二天也好简单呀',
    show: false,
    isLogin: true,
    username: ''
  },
  toggle() {
    this.setData({
      show: !this.data.show
    })
  },
  onShow() {
    // 第二执行的
    console.log("我是onShow")
  },
  onReady() {
    // 第三执行的
    console.log("我是onReady")
  },
  onLoad() {
    // 第一执行的
    console.log('app', app.token)
    console.log("我是onLoad")
    // 举个栗子
    // app.http()
    this.getStudentList()
    // 调用myBehavior里的方法
    this.getVersion()
    console.log('查看behavior数据', this.data.version)
  },
  onHide() {
    console.log("我是onHide")
  },
  onUnload() {
    console.log("我是onUnload")
  },
  // 获取学生列表
  getStudentList() {
    // 调用wx.request
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    wx.request({
      url: 'https://mock.boxuegu.com/mock/3293/students',
      method: "GET",
      success: (res) => {
        console.log(res)
        this.setData({
          students: res.data.result
        })
      },
      complete() {
        // 隐藏loading框
        wx.hideLoading()
      }
    })
  },
  submitForm() {
    console.log(this.data.username)
    // 验证姓名只能为汉字
    const reg = /^[\u4e00-\u9fa5]{2,5}$/
    // 验证输入的内容
    const valid = reg.test(this.data.username.trim())
    // 提示结果
    if (!valid) {
      return wx.showToast({
        title: '姓名只能是2-5位的中文',
        icon: 'none'
      })
    }
  },
  test() {
    console.log("有提示给input事件绑定个空函数就行了")
  },
  setStorageData() {
    // 异步的本地存储
    wx.setStorage({
      key: 'student',
      data: '罗聪聪'
    })
    // 同步的本地存储
    wx.setStorageSync('name', '张玉龙')
    wx.setStorageSync('user', {
      name: '邸克',
      age: 666
    })
  },
  getStorageData() {
    const name = wx.getStorageSync('name')
    const user = wx.getStorageSync('user')
    console.log(name)
    console.log(user)
  },
  removeStorageData() {
    wx.removeStorageSync('name')
  },
  clearStorageData() {
    wx.clearStorageSync()
  }
})